<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增设备类型
            </el-header>
            <el-main>
                <el-form ref="addTypeData" :rules="rules" :model="addTypeData" label-width="115px"
                         label-position="left">
                    <el-form-item label="设备类型 :" prop="name">
                        <el-input v-model="addTypeData.name" placeholder="请输入设备类型"></el-input>
                    </el-form-item>                  
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addType",
        data() {
            return {
                uuid: '',
                addTypeData: {
                    name: ''
                },
                rules: {
                    name: [
                        {required: true, message: '设备类型名称不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
        },
        methods: {
            submit() {
                this.$refs['addTypeData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.addTypeData = replaceSpacesObj(this.addTypeData);
                    this.axios.request({
                        method: "post",
                        url: "./smt/kdi/tyid",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.addTypeData.name
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '设备类型新增失败';
                        this.infoShow = true;
                    });
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanType();
                    this.info = '';
                }, 2000);
            },
            cleanType() {
                this.addTypeData = {
                    name: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanType();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>